package com.cloudptt.api.product.func.call.group.fsm.floor;

import android.content.Context;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.models.call.RtcpMsg;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.CallTimer;
import com.cloudptt.api.product.func.call.ISecondChange;
import com.cloudptt.api.product.func.call.TimerMeter;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;


public class FloorPermissionState implements ISubState, IRollback,ISecondChange
{
	private FloorAgent agent = null;
	private RollbackTimer rollbackTimer = null;
	private int t180 = 1;
	private TimerMeter timerMeter = null;
	private String curGroupNum =  null;
	private int curPri = 0;
	public FloorPermissionState(Context ctx, FloorAgent agent,String groupNum,int pri)
	{
		this.agent = agent;
		Log.d(FloorAgent.Log_Key, "currentState is FloorPermissionState groupNum = " + groupNum + " pri = " + pri);
		this.agent.opAudioMedia(1);
		this.curGroupNum = groupNum;
		this.curPri = pri;
		CallTimer callTimer = agent.getCallTimer();
		this.t180 = callTimer.getT180();
		Log.d(FloorAgent.Log_Key, "currentState is FloorPermissionState t180 = " + t180);
		rollbackTimer = new RollbackTimer(ctx,t180,this);

		this.timerMeter = new TimerMeter(this);
		this.timerMeter.startTiming();
	}

	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Release == btnAction)
		{
			int result = agent.floorRelease();
			if(CauseCont.OperateCheck_Suc == result)
			{
				agent.goToState(CallConst.SpeakState_PendingRelease,null);
			}
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}
	
	public void receiveRtcpMsg(RtcpMsg msg)
	{
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg msg = " + msg);
		if(msg == null)
		{
			return;
		}

		String title = msg.getTitle();
		String groupNum = msg.getValue(RtcpMsg.Key_To);
		if(curGroupNum != null
			&& curGroupNum.equals(groupNum)) {
			if (title.equals(RtcpMsg.Title_FLOOR_REVOKE)) {
				int cause = msg.getCause();
				Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " receiveRtcpMsg cause = " + cause);
				int result = agent.floorRelease();
				if (result == CallConst.Operate_Suc) {
					agent.goToState(CallConst.SpeakState_PendingRelease, null);
				}
			} else if (title.equals(RtcpMsg.Title_FLOOR_GRANTED)) {
			}
			if (title.equals(RtcpMsg.Title_FLOOR_IDLE)) {
				agent.goToState(CallConst.FloorState_NoPermission, null);
			}
		}
	}

	public void updateTakenUser(String callType,AddrInfo gInfo,AddrInfo user)
	{}
	
	public int getState()
	{
		return CallConst.FloorState_Permission;
	}
	public void destory()
	{
//		if(timerMeter != null)
//		{
//			timerMeter.stopTiming();
//			timerMeter = null;
//		}
//		this.agent.opAudioMedia(0);
		if(rollbackTimer != null) {
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}


	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(FloorAgent.Log_Key, "" + CallConst.stateTag(getState()) + " onTimeout");
		if(agent != null)
		{
			agent.floorRelease();
			agent.goToState(CallConst.SpeakState_PendingRelease,null);
		}
	}

	@Override
	public void secondChange(int seconds) {
		// TODO Auto-generated method stub
		if(agent != null)
		{
			agent.updateCallDuration(seconds);
		}
	}
}
